﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Physis.Client.Controllers {

    public class HomeController : ApplicationController {

        public ActionResult Index () {

            if (applicationModel.Application.Session == null) { return null; }

            return View ("~/Views/Home/HomeIndex.cshtml", new Models.ApplicationModel ());

        }

        public ActionResult Login (Int64 id = -1) {

            Models.Home.LoginModel model = new Models.Home.LoginModel ();

            Int64 environmentId = ((id == 0) ? -1 : id); // MAKE SURE THAT ENTERPRISE ONLY IS NOT AVAILABLE 

            model.Authenticate (environmentId); // DEFAULT 

            if (model.AuthenticationResponse.IsAuthenticated) { Response.Redirect ("~/Home"); }

            return View ("~/Views/Home/Login.cshtml", model);

        }

        [HttpGet]
        public JsonResult GlobalSearch (String criteria, String limitTo) {

            // THIS IS A HOLDING PLACE FOR THE FULL SEARCH, ONLY SUPPORTING SPONSORS FOR NOW

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Data.FilterDescriptor> filters = new List<Data.FilterDescriptor> ();


            filters.Add (new Data.FilterDescriptor ("Name", Data.Enumerations.DataFilterOperator.Contains, criteria.Replace ("'", "''")));


            List<Core.Sponsor.Sponsor> sponsors = applicationModel.Application.Core.Sponsor.SponsorsByPage (filters, null, 1, 999999);

            var resultsSponsor = (from currentSponsor in sponsors

                           select new {

                               currentSponsor.Id,

                               ObjectType = "Sponsor",

                               currentSponsor.Name,

                               Detail1 = currentSponsor.SponsorTypeName,

                               Detail2 = currentSponsor.UniqueId,

                               Detail3 = currentSponsor.FederalTaxId,

                               ActionLink = Url.Content ("~/") + "Sponsor/Sponsor/" + currentSponsor.Id

                           }).ToList ();


            List<Core.Insurer.Insurer> insurers = applicationModel.Application.Core.Insurer.InsurersByPage (filters, null, 1, 999999);

            var resultsInsurer = (from currentInsurer in insurers

                                  select new {

                                      currentInsurer.Id,

                                      ObjectType = "Insurer",

                                      currentInsurer.Name,

                                      Detail1 = Data.Functions.PascalString (currentInsurer.InsurerType.ToString()),

                                      Detail2 = currentInsurer.UniqueId,

                                      Detail3 = currentInsurer.FederalTaxId,

                                      ActionLink = Url.Content ("~/") + "Insurer/Insurer/" + currentInsurer.Id

                                  }).ToList ();


            var results = resultsSponsor.Union (resultsInsurer).OrderBy (each => each.Name);

            response.RowsByPage (results);


            return Json (response, JsonRequestBehavior.AllowGet);

        }

    }

}
